home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / QuickDraw™ GX / Programming Stuff / Sample Code / Graphics Samples / ShapePart Browser ƒ / Window.c < prev    next >
Encoding:
Text File  |  1995-04-10  |  8.0 KB  |  360 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    Window.c
  3.  */
  4.  
  5.  
  6. #undef    MAC_HEADERS
  7.  
  8.  
  9. /*------------------*/
  10. /*    Include Files    */
  11. /*------------------*/
  12. #ifndef    MAC_HEADERS
  13.     #include <Windows.h>
  14.     #include <Dialogs.h>
  15.     #include <Menus.h>
  16.     #include <ToolUtils.h>
  17. #endif    MAC_HEADERS
  18.  
  19. #include "graphics routines.h"
  20. #include "graphics libraries.h"    /* For commonColors */
  21.  
  22. #include "ResourceIDs.h"
  23. #include "ShapeAction.h"
  24. #include "ShapeSetup.h"
  25.  
  26. #include "Window.h"
  27.  
  28.  
  29. /*----------------------*/
  30. /*    Global Declarations    */
  31. /*----------------------*/
  32. GlobalStructure    globals;
  33.  
  34.  
  35. /*------------------------------*/
  36. /*    External Declarations     */
  37. /*------------------------------*/
  38.  
  39.  
  40. /*------------------------------*/
  41. /*       Local ProtoTypes       */
  42. /*------------------------------*/
  43. void GetShapePartControls (WindowPtr pWindow, short firstItem, short lastItem, ControlHandle *ph1stControl);
  44. void TrackCheckBox (ControlHandle hControl, Point where, Boolean *pIsChecked);
  45. void SetShapePartsFromFlags (GlobalStructure *pG, ControlHandle hControl, long shapeCount);
  46. void TrackShapePartCheckBox (ControlHandle hControlHit, short whichPart, Point where);
  47.  
  48.  
  49.     void
  50. GetShapePartControls (WindowPtr pWindow, short firstItem, short lastItem, ControlHandle *ph1stControl)
  51. {
  52.     register
  53.     short            item;
  54.     register
  55.     ControlHandle    *pControl;
  56.     short            itemType;
  57.     Rect            itemRect;
  58.  
  59.     pControl = ph1stControl;
  60.     for (item = firstItem; item <= lastItem; item++)
  61.     {
  62.         GetDItem (pWindow, item, &itemType, (Handle *) pControl, &itemRect);
  63.         if (*pControl == nil)
  64.         {
  65.             DebugStr ("\pGetShapePartControls: No checkbox found");
  66.             break;
  67.         }
  68.         pControl++;
  69.     }
  70. }
  71.  
  72.  
  73.     Boolean
  74. InitializeHitTesting (WindowPtr pWindow)
  75. {
  76.     if (! CreateShapesFrame (pWindow, kRowCount, kColumnCount, &globals.boxes))
  77.         return (false);
  78.  
  79.     if (! CreateHitTestShapes (pWindow, &globals.pShapes, &globals.boxes, kShapeCount))
  80.         return (false);
  81.  
  82.     GetShapePartControls (pWindow, checkBoxFirstShapePart, checkBoxLastShapePart, &globals.hNothing);
  83.  
  84.     /* Force SetShapePartsFromFlags to initialize the shape's shape parts */
  85.     globals.partsHit     = gxAnyPart;
  86.     globals.tolerance    = 0;
  87.  
  88.     globals.showControlPoints = false;
  89.     globals.showLocalBounds   = false;
  90.  
  91.     SetShapePartsFromFlags (&globals, globals.hNothing, kShapeCount);
  92.  
  93.     return (true);
  94. }
  95.  
  96.  
  97.     void
  98. CleanupHitTesting (void)
  99. {
  100.     DisposeHitTestShapes (&globals.pShapes, &globals.boxes);
  101. }
  102.  
  103.  
  104.     void
  105. IdleHitTestWindow (WindowPtr pWindow, EventRecord *pEvent)
  106. {
  107.     Point    where;
  108.  
  109.     where = pEvent->where;
  110.     GlobalToLocal (&where);
  111.  
  112.     if (PtInRect (where, &pWindow->portRect))
  113.         UpdateShapePartInfo (&where, &globals);
  114. }
  115.  
  116.  
  117.     void
  118. TrackCheckBox (ControlHandle hControl, Point where, Boolean *pIsChecked)
  119. {
  120.     if (TrackControl (hControl, where, nil))
  121.     {
  122.         *pIsChecked = ! *pIsChecked;
  123.         SetCtlValue (hControl, *pIsChecked);
  124.     }
  125. }
  126.  
  127.  
  128. /*
  129.  *    pG->hNothing
  130.  *    pG->hBounds
  131.  *    pG->hAnything
  132.  *
  133.  *    pG->(all)Checked
  134.  *
  135.  *    pG->partsHit
  136.  */
  137. /*
  138.     Sets all the shape's shapeParts from the state of the check-boxes
  139. */
  140.     void
  141. SetShapePartsFromFlags (GlobalStructure *pG, ControlHandle hControl, long shapeCount)
  142. {
  143.     register
  144.     gxShape            *pShape;
  145.     gxShapePart        newParts;
  146.     Boolean            *pFlag;
  147.     ControlHandle    *pControl;
  148.  
  149.     if (hControl == pG->hNothing)
  150.     {
  151.         if (pG->testNothing)
  152.         {
  153.             /* Nothing checkbox just checked on; turn off all other checkboxes */
  154.             newParts = gxNoPart;
  155.  
  156.             pFlag     = &pG->testBounds;
  157.             pControl = &pG->hBounds;
  158.  
  159.             while (pFlag <= &pG->testAnything)
  160.                 SetCtlValue (*pControl++, *pFlag++ = false);
  161.         }
  162.         else
  163.         {
  164.             /* Nothing was just checked off; turn on all other checkboxes */
  165.             pFlag     = &pG->testBounds;
  166.             pControl = &pG->hBounds;
  167.  
  168.             newParts = gxAnyPart;
  169.             while (pFlag <= &pG->testAnything)
  170.                 SetCtlValue (*pControl++, *pFlag++ = true);
  171.  
  172.             SetCtlValue (pG->hNothing, pG->testNothing = false);
  173.         }
  174.     }
  175.     else if (hControl == pG->hAnything) 
  176.     {
  177.         Boolean        newState;
  178.  
  179.         /* Anything checkbox hit */
  180.         if (pG->testAnything)
  181.         {
  182.             /* Turn on all checkboxes if Anything was already checked */
  183.             newState = true;
  184.             newParts = pG->partsHit | gxAnyPart;
  185.         }
  186.         else
  187.         {
  188.             /* Turn off all checkboxes */
  189.             newState = false;
  190.             newParts = pG->partsHit & ~gxAnyPart;
  191.         }
  192.  
  193.         pFlag     = &pG->testBounds;
  194.         pControl = &pG->hBounds;
  195.  
  196.         while (pFlag <= &pG->testPattern)
  197.             SetCtlValue (*pControl++, *pFlag++ = newState);
  198.  
  199.         /* Turn off the Nothing checkbox */
  200.         SetCtlValue (pG->hNothing, pG->testNothing = false);
  201.     }
  202.     else
  203.     {
  204.         /* An individual checkbox was hit */
  205.         newParts = gxNoPart;
  206.  
  207.         if (pG->testNothing)
  208.             SetCtlValue (pG->hNothing, pG->testNothing = false);
  209.         if (pG->testBounds)            newParts |= gxBoundsPart;
  210.         if (pG->testGeometry)        newParts |= gxGeometryPart;
  211.         if (pG->testPen)            newParts |= gxPenPart;
  212.         if (pG->testCornerPoint)    newParts |= gxCornerPointPart;
  213.         if (pG->testControlPoint)    newParts |= gxControlPointPart;
  214.         if (pG->testEdge)            newParts |= gxEdgePart;
  215.         if (pG->testJoin)            newParts |= gxJoinPart;
  216.         if (pG->testStartCap)        newParts |= gxStartCapPart;
  217.         if (pG->testEndCap)            newParts |= gxEndCapPart;
  218.         if (pG->testDash)            newParts |= gxDashPart;
  219.         if (pG->testPattern)        newParts |= gxPatternPart;
  220.  
  221.         if (pG->testAnything    &&
  222.             (newParts & gxAnyPart) != gxAnyPart)
  223.             SetCtlValue (pG->hAnything, pG->testAnything = false);
  224.     }
  225.  
  226.     if (newParts == gxNoPart)
  227.         SetCtlValue (pG->hNothing, pG->testNothing = true);
  228.     else if ((newParts & gxAnyPart) == gxAnyPart  &&
  229.              (! pG->testAnything))
  230.     {
  231.         SetCtlValue (pG->hAnything, pG->testAnything = true);
  232.     }
  233.  
  234.     /* Set the new parts tested on all shapes */
  235.     for (pShape = pG->pShapes + shapeCount - 1;
  236.          pShape >= pG->pShapes;
  237.          pShape--)
  238.     {
  239.         GXSetShapeHitTest (*pShape, newParts, pG->tolerance);
  240.     }
  241.     pG->partsHit = newParts;
  242. }
  243.  
  244.  
  245.     void
  246. TrackShapePartCheckBox (ControlHandle hControlHit, short whichPart, Point where)
  247. {
  248.     Rect    bounds;
  249.  
  250.     if (hControlHit == globals.hNothing)
  251.     {    TrackCheckBox (globals.hNothing, where, &globals.testNothing);
  252.         return;
  253.     }
  254.  
  255.     if (hControlHit == globals.hBounds)
  256.     {    TrackCheckBox (globals.hBounds, where, &globals.testBounds);
  257.         return;
  258.     }
  259.  
  260.     if (hControlHit == globals.hGeometry)
  261.     {    TrackCheckBox (globals.hGeometry, where, &globals.testGeometry);
  262.         return;
  263.     }
  264.  
  265.     if (hControlHit == globals.hPen)
  266.     {    TrackCheckBox (globals.hPen, where, &globals.testPen);
  267.         return;
  268.     }
  269.  
  270.     if (hControlHit == globals.hCornerPoint)
  271.     {    TrackCheckBox (globals.hCornerPoint, where, &globals.testCornerPoint);
  272.         return;
  273.     }
  274.  
  275.     if (hControlHit == globals.hControlPoint)
  276.     {    TrackCheckBox (globals.hControlPoint, where, &globals.testControlPoint);
  277.         return;
  278.     }
  279.  
  280.     if (hControlHit == globals.hEdge)
  281.     {    TrackCheckBox (globals.hEdge, where, &globals.testEdge);
  282.         return;
  283.     }
  284.  
  285.     if (hControlHit == globals.hJoin)
  286.     {    TrackCheckBox (globals.hJoin, where, &globals.testJoin);
  287.         return;
  288.     }
  289.  
  290.     if (hControlHit == globals.hStartCap)
  291.     {    TrackCheckBox (globals.hStartCap, where, &globals.testStartCap);
  292.         return;
  293.     }
  294.  
  295.     if (hControlHit == globals.hEndCap)
  296.     {    TrackCheckBox (globals.hEndCap, where, &globals.testEndCap);
  297.         return;
  298.     }
  299.  
  300.     if (hControlHit == globals.hDash)
  301.     {    TrackCheckBox (globals.hDash, where, &globals.testDash);
  302.         return;
  303.     }
  304.  
  305.     if (hControlHit == globals.hPattern)
  306.     {    TrackCheckBox (globals.hPattern, where, &globals.testPattern);
  307.         return;
  308.     }
  309.  
  310.     TrackCheckBox (globals.hAnything, where, &globals.testAnything);
  311. }
  312.  
  313.  
  314.     void
  315. DoHitTestContent (EventRecord *pEvent, WindowPtr pWindow)
  316. {
  317.     Point            where;
  318.     ControlHandle    hControl;
  319.     short            partCode;
  320.  
  321.     where = pEvent->where;
  322.     GlobalToLocal (&where);
  323.  
  324.     partCode = FindControl (where, pWindow, &hControl);
  325.  
  326.     if (hControl != nil)
  327.     {
  328.         TrackShapePartCheckBox (hControl, partCode, where);
  329.         SetShapePartsFromFlags (&globals, hControl, kShapeCount);
  330.     }
  331. }
  332.  
  333.  
  334.     void
  335. ToggleMetricsMenuItem (short item, WindowPtr pWIndow)
  336. {
  337.     Boolean        *pState;
  338.     Str255        menuItemStr;
  339.  
  340.     switch (item)
  341.     {
  342.         case itemShowControlPoints:
  343.             pState = &globals.showControlPoints;
  344.             break;
  345.  
  346.         case itemShowLocalBounds:
  347.             pState = &globals.showLocalBounds;
  348.             break;
  349.     }
  350.  
  351.     *pState = ! *pState;
  352.     GetIndString (menuItemStr, metricsMenuStringsID, *pState + 2 * item - itemShowControlPoints);
  353.     SetItem (GetMHandle (metricsMenuRsrcID), item, menuItemStr);
  354.  
  355.     if (*pState  &&  item == itemShowControlPoints)
  356.         ShowControlPoints (globals.pShapes, kShapeCount);
  357.     else
  358.         UpdateHitTestWindow (pWIndow);
  359. }
  360.